package com.cn.hospital.controller.auth;

import cn.dev33.satoken.stp.StpUtil;
import com.cn.hospital.message.Msg;
import com.cn.hospital.service.auth.LogoutService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LogoutController {

    @Reference
    LogoutService logoutService;

    /**
     * 注销
     *
     * @return
     */
    @RequestMapping("/logout")
    public Msg logout() {
        return verifyLogout(logoutService.logout(getStpMap()));
    }

    /**
     * 确认信息无误注销
     *
     * @param msg
     * @return
     */
    public Msg verifyLogout(Msg msg) {
        if (msg.getCode().equals(200)) {
            StpUtil.logout();
        }
        return msg;
    }

    /**
     * 初始化stp数据
     *
     * @return
     */
    public Map<String, Object> getStpMap() {
        Map<String, Object> stpMap = new HashMap<>();
        stpMap.put("loginId", StpUtil.getLoginId());
        stpMap.put("stpSaTokenValue", StpUtil.getTokenValue());
        return stpMap;
    }
}
